//
//  OfficialInformationVController.swift
//  AirTravel
//
//  Created by apple on 2021/8/24.
//

import UIKit
import MessageUI
import Moya
import SwiftyJSON

class OfficialInformationVController: UIViewController {

    @IBOutlet weak var backView: UIView!
    @IBOutlet weak var completeBtn: UIButton!
    @IBOutlet weak var inputTF: UITextField!
    var picker = MFMessageComposeViewController()
    /// 用来主动取消网络请求
    var cancelableRequest: Cancellable?
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.tintColor = UIColor.black
        self.navigationController?.navigationBar.titleTextAttributes = [
            NSAttributedString.Key.foregroundColor : UIColor.black]
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.navigationController?.navigationBar.tintColor = UIColor.white
        self.navigationController?.navigationBar.titleTextAttributes = [
            NSAttributedString.Key.foregroundColor : UIColor.white]
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "信息验证"
        self.completeBtn.layer.cornerRadius = 6
        self.completeBtn.layer.masksToBounds = true
        self.backView.layer.cornerRadius = 6
        self.backView.layer.masksToBounds = true
        
        
    }

    //发送验证码
    @IBAction func sendCode(_ sender: Any) {
        if MFMessageComposeViewController.canSendText() {
            self.picker.messageComposeDelegate = self
            self.picker.recipients = ["12306"]
            self.picker.body = "666"
            self.picker.navigationBar.tintColor = Red_COLOR
            self.present(self.picker, animated: true, completion: nil)
        }
    }
    
    @IBAction func complete(_ sender: Any) {
        let code = "\(self.inputTF.text ?? "")"
        if code.isEmpty {
            YProgressHUD.showToast("验证码不能为空")
        }
        let username = "\(UserDefaults.standard.value(forKey: "12306Login") ?? "")"
        cancelableRequest = NetWorkRequest(API1.smsVerify12306Account(parameters: ["username":username,"code":code]), completion: { (responseString) -> (Void) in
            YProgressHUD.showToast("核验码成功")
            self.navigationController?.popViewController(animated: true)
        }, failed: { (failedResutl) -> (Void) in
            print("服务器返回code不为0000啦~\(failedResutl)")
        }, errorResult: { () -> (Void) in
            print("网络异常")
        })
    }
    
    
}

extension OfficialInformationVController:MFMessageComposeViewControllerDelegate{
    func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
        switch result {
        case .sent://成功
            self.dismiss(animated: true, completion: nil)
        case .failed://失败
            self.dismiss(animated: true, completion: nil)
        case .cancelled://取消
            self.dismiss(animated: true, completion: nil)
        default:
            self.dismiss(animated: true, completion: nil)
        }
    }
    
    
}
